/*
 * Thumbnailator - a thumbnail generation library
 *
 * Copyright (c) 2008-2020 Chris Kroells
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.lambert.gingko.watermark.enums;

import com.lambert.gingko.watermark.model.PositionParam;

import java.awt.*;

/**
 * 预定义的{@link Position}枚举。
 * 
 * @author lambert
 *
 */
public enum Positions implements Position {
	/**
	 * 计算被包含元素应放置在容器左上角时的{@link Point}坐标。
	 */
	TOP_LEFT() {
		@Override
		public Point calculate(PositionParam positionParam) {
			// 左内边距
			int insetLeft = positionParam.getInsetLeft();
			// 上内边距
			int insetTop = positionParam.getInsetTop();
			// X坐标=左内边距（左对齐）
			int x = insetLeft;
			// Y坐标=上内边距（上对齐）
			int y = insetTop;
			return new Point(x, y);
		}
	},

	/**
	 * 计算被包含元素应水平居中放置在容器顶部时的{@link Point}坐标。
	 */
	TOP_CENTER() {
		@Override
		public Point calculate(PositionParam positionParam) {
			// 容器宽度
			int enclosingWidth = positionParam.getEnclosingWidth();
			// 元素宽度
			int width = positionParam.getWidth();
			// 上内边距
			int insetTop = positionParam.getInsetTop();
			// X坐标=容器中点-元素中点（水平居中）
			int x = (enclosingWidth / 2) - (width / 2);
			int y = insetTop;   // Y坐标=上内边距（上对齐）
			return new Point(x, y);
		}
	},

	/**
	 * 计算被包含元素应放置在容器右上角时的{@link Point}坐标。
	 */
	TOP_RIGHT() {
		@Override
		public Point calculate(PositionParam positionParam) {
			// 容器宽度
			int enclosingWidth = positionParam.getEnclosingWidth();
			// 元素宽度
			int width = positionParam.getWidth();
			// 右内边距
			int insetRight = positionParam.getInsetRight();
			// 上内边距
			int insetTop = positionParam.getInsetTop();
			// X坐标=容器宽度-元素宽度-右内边距（右对齐）
			int x = enclosingWidth - width - insetRight;
			int y = insetTop;   // Y坐标=上内边距（上对齐）
			return new Point(x, y);
		}
	},

	/**
	 * 计算被包含元素应垂直居中放置在容器左侧时的{@link Point}坐标。
	 */
	CENTER_LEFT() {
		@Override
		public Point calculate(PositionParam positionParam) {
			// 左内边距
			int insetLeft = positionParam.getInsetLeft();
			// 容器高度
			int enclosingHeight = positionParam.getEnclosingHeight();
			// 元素高度
			int height = positionParam.getHeight();
			// X坐标=左内边距（左对齐）
			int x = insetLeft;
			// Y坐标=容器中点-元素中点（垂直居中）
			int y = (enclosingHeight / 2) - (height / 2);
			return new Point(x, y);
		}
	},

	/**
	 * 计算被包含元素应水平和垂直居中放置在容器中时的{@link Point}坐标。
	 */
	CENTER() {
		@Override
		public Point calculate(PositionParam positionParam) {
			// 容器宽度
			int enclosingWidth = positionParam.getEnclosingWidth();
			// 容器高度
			int enclosingHeight = positionParam.getEnclosingHeight();
			// 元素宽度
			int width = positionParam.getWidth();
			// 元素高度
			int height = positionParam.getHeight();
			// X坐标=容器中点-元素中点（水平居中）
			int x = (enclosingWidth / 2) - (width / 2);
			// Y坐标=容器中点-元素中点（垂直居中）
			int y = (enclosingHeight / 2) - (height / 2);
			return new Point(x, y);
		}
	},

	/**
	 * 计算被包含元素应垂直居中放置在容器右侧时的{@link Point}坐标。
	 */
	CENTER_RIGHT() {
		@Override
		public Point calculate(PositionParam positionParam) {
			// 容器宽度
			int enclosingWidth = positionParam.getEnclosingWidth();
			// 容器高度
			int enclosingHeight = positionParam.getEnclosingHeight();
			// 元素宽度
			int width = positionParam.getWidth();
			// 元素高度
			int height = positionParam.getHeight();
			// 右内边距
			int insetRight = positionParam.getInsetRight();
			// X坐标=容器宽度-元素宽度-右内边距（右对齐）
			int x = enclosingWidth - width - insetRight;
			// Y坐标=容器中点-元素中点（垂直居中）
			int y = (enclosingHeight / 2) - (height / 2);
			return new Point(x, y);
		}
	},

	/**
	 * 计算被包含元素应放置在容器左下角时的{@link Point}坐标。
	 */
	BOTTOM_LEFT() {
		@Override
		public Point calculate(PositionParam positionParam) {
			// 左内边距
			int insetLeft = positionParam.getInsetLeft();
			// 容器高度
			int enclosingHeight = positionParam.getEnclosingHeight();
			// 元素高度
			int height = positionParam.getHeight();
			// 下内边距
			int insetBottom = positionParam.getInsetBottom();
			// X坐标=左内边距（左对齐）
			int x = insetLeft;
			// Y坐标=容器高度-元素高度-下内边距（下对齐）
			int y = enclosingHeight - height - insetBottom;
			return new Point(x, y);
		}
	},

	/**
	 * 计算被包含元素应水平居中放置在容器底部时的{@link Point}坐标。
	 */
	BOTTOM_CENTER() {
		@Override
		public Point calculate(PositionParam positionParam) {
			// 容器宽度
			int enclosingWidth = positionParam.getEnclosingWidth();
			// 容器高度
			int enclosingHeight = positionParam.getEnclosingHeight();
			// 元素宽度
			int width = positionParam.getWidth();
			// 元素高度
			int height = positionParam.getHeight();
			// 下内边距
			int insetBottom = positionParam.getInsetBottom();
			// X坐标=容器中点-元素中点（水平居中）
			int x = (enclosingWidth / 2) - (width / 2);
			// Y坐标=容器高度-元素高度-下内边距（下对齐）
			int y = enclosingHeight - height - insetBottom;
			return new Point(x, y);
		}
	},

	/**
	 * 计算被包含元素应放置在容器右下角时的{@link Point}坐标。
	 */
	BOTTOM_RIGHT() {
		@Override
		public Point calculate(PositionParam positionParam) {
			// 容器宽度
			int enclosingWidth = positionParam.getEnclosingWidth();
			// 容器高度
			int enclosingHeight = positionParam.getEnclosingHeight();
			// 元素宽度
			int width = positionParam.getWidth();
			// 元素高度
			int height = positionParam.getHeight();
			// 右内边距
			int insetRight = positionParam.getInsetRight();
			// 下内边距
			int insetBottom = positionParam.getInsetBottom();
			// X坐标=容器宽度-元素宽度-右内边距（右对齐）
			int x = enclosingWidth - width - insetRight;
			// Y坐标=容器高度-元素高度-下内边距（下对齐）
			int y = enclosingHeight - height - insetBottom;
			return new Point(x, y);
		}
	},

}
